//
//  YDAccountSettingController.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2017/11/13.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "YDAccountSettingController.h"
#import "YDAccountSettingCell.h"
#import "YDModifyPasswordController.h"//修改密码
#import "YDBindBankCardController.h"//绑定银行卡
#import "SYUploadAvaterManager.h"
#import "HXPhotoPicker.h"
#import "YDModifyPayPasswordViewController.h" //修改支付密码
#import "YDPaymentPasswordController.h"
#import "YDBankListViewController.h"//我的银行卡列表
#import "YDRealNameAuthController.h"

@interface YDAccountSettingController ()<UITableViewDelegate , UITableViewDataSource>

//tableView
@property (nonatomic , weak) UITableView *tableView;

//用户手机号
@property (nonatomic , copy) NSString *userPhoneNumber;
//是不是已经绑卡
@property (nonatomic , assign) BOOL isBindCard;
//是不是已经设置支付密码
@property (nonatomic , assign) BOOL isSeetingPayPass;
//有咩有实名认证
@property (nonatomic , assign) BOOL isRealName;
//绑卡个数
@property (nonatomic, assign)NSInteger bankCount;

//选择照片
@property (strong, nonatomic) HXPhotoManager *manager;

//网络占位图
@property (nonatomic, weak) SYNoNetView *noNetView;

@end

@implementation YDAccountSettingController

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = NumberColor(247, 247, 247);
    self.title = @"账户设置";
    [self tableView];
    [self setUpDataManger];
    
    [self loadUserInformation];
    
}

-(void)setUpDataManger
{
    MJGIFHeader *header = [MJGIFHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadUserInformation)];
    //隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES ;
    //隐藏状态
    header.stateLabel.hidden = YES ;
    self.tableView.mj_header = header;
}

///*********加载数据***********/////
-(void)loadUserInformation
{
    
    [[SYProgressHUD sharedInstance] showMBHudWithSting:kLoadingText];
    
    [BaseNetRequest sessionRequestWithparameters:nil method:GETMethod requstType:GetSettingInformationList WithSuccessBlock:^(NSDictionary *resultDict) {
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        [self.tableView.mj_header endRefreshing];
        DDLog(@"%@",resultDict)
        DDLog(@"%@",resultDict[@"message"])
        if ([resultDict[@"code"] integerValue] == 200) {
            [self removeNoNetView];
            //手机号注册的，必须有手机号
            self.userPhoneNumber = resultDict[@"result"][@"phone"];
            
            self.isBindCard = [resultDict[@"result"][@"settingBindCard"] boolValue];
            
            self.isSeetingPayPass = [resultDict[@"result"][@"settingPaymentPassword"] boolValue];
            
            self.bankCount =  [resultDict[@"result"][@"bindCardCount"] integerValue];
            self.isRealName = [resultDict[@"result"][@"settingRealName"] integerValue];
            
            [self.tableView reloadData];
            
        }else{

            [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"信息加载出错，请重试~" isExistNav:YES];
            [self noNetView];
        }
    } failBlock:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        DDLog(@"error = %@",error)
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];

         [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"信息加载出错，请重试~" isExistNav:YES];
        [self noNetView];
    }];
}
- (void)removeNoNetView
{
    if (_noNetView != nil) {
        [_noNetView removeFromSuperview];
        _noNetView = nil;
    }
}
-(SYNoNetView *)noNetView
{
    if (_noNetView == nil) {
        SYNoNetView *noNetView = [[SYNoNetView alloc] init];
        [self.tableView addSubview:noNetView];
        
        [noNetView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(self.view);
            //            make.top.equalTo(self.view);
        }];
        LK(weself)
        [noNetView setClickBlock:^(BOOL isClick) {
            [weself loadUserInformation];
            
        }];
        _noNetView = noNetView;
    }
    return _noNetView;
}


/*******tableView********/
-(UITableView *)tableView
{
    if (_tableView == nil) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        [self.view addSubview:tableView];
        tableView.backgroundColor = NumberColor(247, 247, 247);
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        if (isIos11) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView = tableView;
    }
    return _tableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    if (section == 1) {
        return 2;
    }else if (section == 2){
        return 3;
    }else{
        return 1;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return isPhoneX ? rateX812(50) : rate750(50);
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return isPhoneX ? rateX812(10) : rate750(10);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = NumberColor(247, 247, 247);
    return view;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *phoneNumber = [NSString phoneNumberasterisk:self.userPhoneNumber];
    
    YDAccountSettingCell *cell = [YDAccountSettingCell cellWithTableView:tableView];
    //隐藏设置状态 section 2
    //隐藏设置状态 section 2
    if (indexPath.section != 2) {
        cell.setStatus.hidden = YES;
    }else{
        cell.setStatus.hidden = NO;
        
    }
    
    //隐藏手机号
    if (indexPath.section == 1 && indexPath.row == 0) {
        cell.phoneLabel.hidden = NO;
        cell.phoneLabel.text = phoneNumber;
    }else{
        cell.phoneLabel.hidden = YES;
        
    }
    
    if (indexPath.section == 0) {
        
        // cell.accImage.hidden = NO;
        if (indexPath.item == 0) {
            cell.imageName = @"wd-zhsz-shangchuantouxiang";
            cell.cateString = @"上传/更换头像";
            cell.accImage.hidden = NO;
        }
        
    }
    else if (indexPath.section == 1) {
        if (indexPath.item == 0) {
            cell.imageName = @"wd-zhsz-shoujibangding";
            cell.cateString = @"手机绑定";
            cell.accImage.hidden = YES;
            
        }else{
            cell.imageName = @"wd-zhsz-xiugaimima";
            cell.cateString = @"修改密码";
            cell.accImage.hidden = NO;
            
        }
        
    }else if (indexPath.section == 2){
        
        if (indexPath.item == 0) {
            cell.imageName = @"wd-zhsz-yinhangka";
            cell.cateString = @"银行卡";
            cell.accImage.hidden = NO;
            cell.isSet = self.isBindCard;
            cell.bankCount = [NSString stringWithFormat:@"%ld",self.bankCount];
        }else if (indexPath.item == 1){
            cell.imageName = @"wd-zhsz-zhifumima";
            cell.cateString = @"支付密码";
            cell.accImage.hidden = NO;
            cell.isSet = self.isSeetingPayPass;
        }else{
            cell.imageName = @"pzshimingrenzheng";
            cell.cateString = @"实名认证";
            
            cell.setStatus.text = self.isRealName ? @"已认证" :@"未认证";
            cell.accImage.hidden = self.isRealName;
            
        }
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        //设置头像
       [self hx_presentAlbumListViewControllerWithManager:self.manager delegate:self];
    }
    
    else if (indexPath.section == 1) {
        if (indexPath.item == 0) {//手机绑定
        }else{//修改密码
            YDModifyPasswordController *vc = [[YDModifyPasswordController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
       
    }else if (indexPath.section == 2){
        if (indexPath.item == 0) {//银行卡
            
            if (self.isBindCard) {
                YDBankListViewController *vc = [[YDBankListViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
            else
            {
                YDBindBankCardController *vc = [[YDBindBankCardController alloc] init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
            
           
        }else if (indexPath.item == 1){//支付密码
            
            if ( self.isSeetingPayPass) {
                //修改支付密码
                YDModifyPayPasswordViewController *vc = [[YDModifyPayPasswordViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
            else
            {
                //设置支付密码
                YDPaymentPasswordController *vc = [[YDPaymentPasswordController alloc] init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
                
            }
            
        }else{
            //实名认证
            if (self.isRealName)return;
            YDRealNameAuthController *vc = [[YDRealNameAuthController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}



- (void)albumListViewController:(HXAlbumListViewController *)albumListViewController didDoneAllList:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photoList videos:(NSArray<HXPhotoModel *> *)videoList original:(BOOL)original {
    if (photoList.count > 0) {
         HXPhotoModel *model = photoList.firstObject;
      //  [[PZProgressHUD sharedInstance] showMBHudWithSting:@"上传中~"];
        [SYUploadAvaterManager startUploadFinshWithImage:model.previewPhoto finish:^(NSString *url) {
     
           //  [[PZProgressHUD sharedInstance] dismissMBWithTime:0];
        
            
            if ([url isEqualToString:@"error"]) {
               
                [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"上传头像失败" isExistNav:YES];
            }
            else
            { [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"上传头像成功" isExistNav:YES];
                DDLog(@"头像成功地址====%@",url);
            }
        }];
        //TODO: FIX 上传照片
        
    }
}
- (HXPhotoManager *)manager {
    if (!_manager) {
        // __weak typeof(self) weakSelf = self;
        //HXPhotoManagerSelectedType 选择媒体资源的类型
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
        _manager.configuration.singleSelected = YES;// 是否为单选模式 默认 NO , 会自动过滤掉gif、livephoto
        _manager.configuration.albumListTableView = ^(UITableView *tableView) {
            //            NSSLog(@"%@",tableView);
        };
        
        _manager.configuration.themeColor = [UIColor whiteColor];
        
        _manager.configuration.deleteTemporaryPhoto = YES;//是佛删除拍摄的照片或视频
        _manager.configuration.saveSystemAblum = NO;//拍摄的视频或者相片是否保存到相册
        _manager.configuration.supportRotation = YES;//是否支持旋转
        _manager.configuration.cameraCellShowPreview = YES;//相机cell是否支持预览，默认为yes
        
        //设置弹出的相册选择界面导航栏的背景图片
        _manager.configuration.navigationTitleColor = [UIColor whiteColor];
        _manager.configuration.navigationBar = ^(UINavigationBar *navigationBar) {
            [navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"0E4483"]] forBarMetrics:UIBarMetricsDefault];
            navigationBar.barTintColor = [UIColor whiteColor];
            
        };
        _manager.configuration.statusBarStyle = UIStatusBarStyleLightContent;
        _manager.configuration.movableCropBox = YES;
        _manager.configuration.movableCropBoxEditSize = YES;
        _manager.configuration.movableCropBoxCustomRatio = CGPointMake(1, 1);//可移动裁剪框的宽高比

    }
    return _manager;
}
@end
